const { spawn } = require('child_process')

function callPythonScript (scriptPath, args) {
  return new Promise((resolve, reject) => {
    let responseData = ''

    const pythonProcess = spawn('python', [scriptPath, ...args])

    pythonProcess.stdout.on('data', (data) => {
      responseData += data.toString()
    })

    pythonProcess.on('close', (code) => {
      if (code === 0) {
        resolve(JSON.parse(responseData))
      } else {
        reject(responseData)
      }
    })
  })
}

module.exports = callPythonScript
